<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-04-27 14:31:19
 * @LastEditTime: 2022-04-27 14:39:11
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="test"></div>
  <script type="text/javascript" src="../js/新/react.development.js"></script>
  <script type="text/javascript" src="../js/新/react-dom.development.js"></script>
  <script type="text/javascript" src="../js/新/babel.min.js">
  </script>
  <script src="../js/新/prop-types.js"></script>
  <script type="text/babel">
    class Students extends React.Component {
      state = {
        list: [
          { id: 1, name: '张三', age: 12 },
          { id: 2, name: '李四', age: 13 },
        ]
      }
      add = () => {
        this.setState({
          list: [
            { id: 3, name: '小王', age: 14 },
            ...this.state.list
          ]
        })
      }
      render() {
        return (
          <div>
            <h3>使用 index 作为 key</h3>
            <ul>
              {
                this.state.list.map((item, index) => <li key={index}>{'姓名:' + item.name + '---年龄:' + item.age}<input type='text' /></li>)
              }
            </ul>
            <button onClick={this.add}>添加小王</button>
            <hr /><hr />
            <h3>使用 id 作为 key</h3>
            <ul>
              {
                this.state.list.map((item, index) => <li key={item.id}>{'姓名:' + item.name + '---年龄:' + item.age}<input type='text' /></li>)
              }
            </ul>
            <button onClick={this.add}>添加小王</button>
          </div>
        )
      }
    }
    ReactDOM.render(<Students />, document.querySelector('#test'))
  </script>
</body>

</html>